limsOrderTask.sampleTaskSearchDetailPage = {

    getBtnToolbar: function (runId) {
        var _this = this;
        var items = [{
            type: 'button',
            text: 'GIKAM.BUTTON.BACK',
            icon: 'back',
            onClick: function () {
                if (_this.param.modal) {
                    Gikam.getLastModal().close()
                } else {
                    workspace.window.goBack()
                }
            }
        },]
        return {
            type: 'btnToolbar',
            items: items
        }
    },

    loadBatchProList: function (row) {
        //模板工序
        Gikam.getAsyncComp('template-list-grid').done(function (grid) {
            grid.refresh({
                url: limsOrderTask.preOrderTaskUrl + '/sample-task/search',
                requestData: {
                    batchSampleId_EQ: row.ext$.batchsampleid
                }
            })
        })
    },

    getPreTaskInfoGrid: function () {
        var _this = this
        let params = {
            batchSampleId_EQ: _this.param.batchSampleId
        }
        if (_this.param.productId) {
            params.productId_EQ = _this.param.productId
        }
        return {
            type: 'grid',
            id: 'template-list-grid',
            checkOnActive: true,
            columns: limsOrderTask.getTemplateInfoColumns(),
            group: {
                // groupCheckbox : true,
                fields: ['folderId', 'ext$.etname'],
                formatter: function (row) {
                    return row.folderId + "：" + row.ext$.etname
                }
            },
            order: {
                'folderId': 'asc',
                'ext$.etname': 'asc',
                'sort': 'asc'
            },
            url: limsOrderTask.preOrderTaskUrl + '/sample-task/search',
            requestData: params,
            // 选中行
            onRowActive: function (index, row) {
                _this.loadPreTaskList(row)
            },
            onLoadSuccess: function (rows) {
                if (Gikam.isNotEmpty(rows)) {
                    this.activeRowByIndex(0)
                } else {
                    _this.loadPreTaskList()
                }
            },
            toolbar: [],
        }

    },


    loadPreTaskList: function (row) {
        var _this = this
        Gikam.getAsyncComp('task-info-list-grid').done(function (grid) {
            if (row) {
                grid.refresh({
                    url: limsOrderTask.baseUrl + '/queries',
                    requestData: {
                        preOrderTaskId_EQ: row.id,
                    }
                })
            } else {
                grid.cleanData()
            }
        })
    },


    // 任务信息
    getTaskInfoGrid: function () {
        var _this = this
        return {
            type: 'grid',
            id: 'task-info-list-grid',
            checkOnActive: true,
            service: 'limsOrderTaskServiceImpl',
            columns: limsOrderTask.getTaskInfoColumns(),
            onRowActive: function (index, row) {
                _this.reloadResult(row)
            },
            onLoadSuccess: function (rows) {
                if (Gikam.isNotEmpty(rows)) {
                    this.activeRowByIndex(0)
                } else {
                    _this.reloadResult()
                }
            },
            onCellClick: function (field, row) {
                field === 'runCode' && _this.loadRunOrderTaskPage(row);
            },
            toolbar: [],
        }
    },

    loadRunOrderTaskPage: function (row) {
        if (Gikam.isEmpty(row) || Gikam.isEmpty(row.runId) || Gikam.isEmpty(row.runCode)) {
            return false;
        }
        Gikam.create('modal', {
            title: 'LIMS.MODAL.RUNDETAIL',
            url: limsResult.baseUrl + '/page/run-order-task-readonly-detail' + Gikam.param({
                id: row.runId,
                runCode: row.runCode,
                // 以弹窗的形式展示详情，各组件为只读模式
                readonly: true,
                status: 'all'
            })
        });
    },

    reloadResult: function (row) {
        var _this = this
        Gikam.getAsyncComp('tj-order-task-result-grid').done(function (grid) {
            if (row) {
                grid.refresh({
                    url: limsResult.baseUrl + '/queries/raw',
                    requestData: {
                        orderTaskId_EQ: row.id,
                    }
                })
            } else {
                grid.cleanData()
            }
        })

        Gikam.getAsyncComp('order-task-derivative-grid').done(function (grid) {
            if (row) {
                grid.refresh({
                    url: Gikam.printf(limsResult.derivativelUrl + '/tasks/{taskId}/queries', {
                        taskId: row.id
                    }),
                })
            } else {
                grid.cleanData()
            }
        })

        if (row) {
            Gikam.getAsyncComp('result-uploader-task-search-detail').done(function (uploader) {
                uploader.setOptions({
                    bizId: row.runCode
                })
            })
        } else {
            Gikam.getAsyncComp('result-uploader-task-search-detail').done(function (uploader) {
                uploader.setOptions({
                    bizId: -1
                })
            })
        }

        Gikam.getAsyncComp('batch-sample-record-list-grid').done(function (grid) {
            if (row) {
                grid.refresh({
                    url: limsResult.auditRecordUrl + '/queries',
                    requestData: {
                        businessId_SEQ: row.id,
                        businessCode_SEQ: 'T_LIMS_ORDER_TASK'
                    }
                })
            } else {
                grid.cleanData()
            }
        })
    },

    getAnalyteDerivativeTab: function () {
        var _this = this
        return {
            type: 'tab',
            id: 'result-order-task-result-tab',
            panels: [{
                title: 'T_CORE_ITEM.SERVICE_NAME',
                id: 'result-order-task-result-panel',
                items: [{
                    type: 'grid',
                    id: 'tj-order-task-result-grid',
                    page: false,
                    order: {
                        sorter: 'asc'
                    },
                    service: 'resultServiceImpl',
                    columns: limsResult.getResultListColumns(),
                }]
            }, {
                title: 'MODULE.BUSINESS.RESULT.TAB.DERIVATIVE',
                id: 'result-order-task-derivative-panel',
                items: [{
                    type: 'grid',
                    id: 'order-task-derivative-grid',
                    page: false,
                    service: 'sampleServiceImpl',
                    columns: limsResult.getAllDerivativeEditListColumns(),
                }]
            }, {
                title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
                id: 'result-order-task-history-panel',
                items: [{
                    type: 'grid',
                    id: 'batch-sample-record-list-grid',
                    columns: limsResult.getFlowListColumns(),
                }]
            }, {
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    id: 'result-uploader-task-search-detail',
                    filter: false,
                    dbTable: 'T_LIMS_RUN',
                    bizId: -1,
                    readonly: true
                }]
            }]
        }
    },

    urlManager: {
        orderTaskGrid: IFM_CONTEXT + '/secure/base-module/business/order-tasks',
        runGrid: limsOrderTask.runUrl,
        resultGrid: limsOrderTask.baseUrl,
        derivativeGrid: IFM_CONTEXT + '/secure/base-module/common/samples'
    },


    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'result-edit-layout',
            renderTo: workspace.window.$dom,
            west: {
                width: '40%',
                title: 'T_LIMS_EXPERIMENTAL_TEMPLATE_TEST_METHOD.SERVICE_NAME',
                items: [this.getBtnToolbar(), _this.getPreTaskInfoGrid()]
            },
            center: {
                items: [{
                    type: 'layout',
                    north: {
                        height: '40%',
                        title: 'T_LIMS_ORDER_TASK.SERVICE_NAME',
                        items: [_this.getTaskInfoGrid()]
                    },
                    center: {
                        items: [_this.getAnalyteDerivativeTab()]
                    }
                }]
            }
        })
    },

    init: function (param) {
        this.param = param
        this.create()
    }
}
